package com.exonline.backstage.controller.menu;

import cn.hutool.core.util.IdUtil;
import com.exonline.commons.base.IBaseServlet;

import com.exonline.backstage.entity.Menu;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/admin/menu")
public class MenuServlet extends HttpServlet implements IBaseServlet {
    ObjectMapper mapper = new ObjectMapper();
    MenuService service = new MenuService();
    int pageno = 1;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageno = Integer.parseInt(req.getParameter("pageno"));
        String method = req.getParameter("method");
        try {
            if ("query".equals(method)) {
                this.query(req, resp);
            } else if ("queryone".equals(method)) {
                this.queryOne(req, resp);
            } else if ("add".equals(method)) {
                this.add(req, resp);
            } else if ("edit".equals(method)) {
                this.edit(req, resp);
            } else if ("delete".equals(method)) {
                this.delete(req, resp);
            }
        } catch (Exception e) {
            throw new ServletException("角色菜单模块出现错误,错误信息:"+e.getMessage()+e);
        }
    }


    @Override
    public void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        List<Menu> menus = service.queryAll(10, pageno);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        map.put("data", menus);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        service.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", id);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        Menu menu = service.queryOne(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", menu);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String url = req.getParameter("url");
        service.update(name, url, id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "修改成功");
        map.put("data", id);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = IdUtil.simpleUUID();
        String name = req.getParameter("name");
        String url = req.getParameter("url");
        service.add(id,name,url);
         Map<String, Object> map = new HashMap<>();
                 map.put("code", 200);
                 map.put("msg", "添加成功");
                 map.put("data", id);
                 mapper.writeValue(resp.getWriter(), map);
    }
}
